package com.sk.bilibili.view.diy.bottomDialog;

import android.view.View;
import android.widget.Toast;

import androidx.viewbinding.ViewBinding;

import com.sk.bilibili.dto.GenericInfo;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.utils.GenericUtils;
import com.sk.bilibili.view.BaseView;

import java.lang.reflect.Constructor;

import lombok.SneakyThrows;

public abstract class BaseBottomDialog<M extends BaseModel> extends me.shaohui.bottomdialog.BaseBottomDialog implements
        View.OnClickListener, BaseView {

    protected M model;

    public BaseBottomDialog() {
        initModel();
    }

    @SneakyThrows
    private void initModel() {
        GenericInfo<ViewBinding, M> info = GenericUtils.parse(getClass());
        Class<M> modelClass = info.getModelClass();
        model=modelClass.newInstance();
        model.setView(this);
    }

    @Override
    public void bindView(View v) {
        int[] ids=getClickIds();
        for (int id:ids){
            v.findViewById(id).setOnClickListener(this);
        }
    }

    protected abstract int[] getClickIds();

    @Override
    public void onClick(View v) {
        onClick(v.getId());
    }

    protected abstract void onClick(int id);

    @Override
    public void loadError() {

    }

    protected void toast(String msg){
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

}